package com.bdbit.ChargingStation.controller;

import com.bdbit.ChargingStation.controller.resp.RespObj;
import com.bdbit.ChargingStation.services.WxService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 微信服务API
 */
@RestController
@RequestMapping("/wx")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class WxController {

    private final WxService wxService;
    /**
     * 网页授权
     * 通过code换取网页授权access_token
     * 通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token（该access_token用于调用其他接口）不同.
     * <a href="https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#1">网页授权</a>
     */
    @GetMapping("/getAccessToken")
    public RespObj<String> getWebsiteAccesstoken(@RequestParam String code) {
        RespObj<String> respObj = new RespObj<>();
        try {
            respObj.setCode(200);
            respObj.setMessage("success");
            respObj.setData(wxService.getAccessToken(code));
        } catch (IOException e) {
            respObj.setCode(-1);
            respObj.setMessage("failed");
        }
        return respObj;
    }

}
